package com.qimingyu.linkedlist.copyrandomlist;

// Definition for a Node.
class ListNode {
    int val;
    ListNode next;
    ListNode random;

    public ListNode(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
public class CopyRandomList {
    public ListNode copyRandomList(ListNode head) {
        ListNode p2 = head;
        ListNode newHead = null;
        ListNode p1 = newHead;
        while(p2 != null){
            if(p1 == null){
                p1 = new ListNode(p2.val);
            }
            else{
                while (p1.next != null) {
                    p1 = p1.next;
                }
                p1.next = new ListNode(p2.val);
            }
            p2 = p2.next;
        }
        return newHead;
    }

    public static void main(String[] args) {

    }
}
